package Statistics;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.Iterator;

import com.sleepycat.collections.StoredEntrySet;

import EurekaAnalyzer.IDInformation;
import Store.IDInfoKey;


/**
 * @author Xenophon
 * @version 2009-12-26
 */
public class IDGraph {
	private HashMap<String, IDGraphNode> nodeMap;
	
	public IDGraph(Map pool)
	{
		nodeMap = new HashMap<String, IDGraphNode>();
		StoredEntrySet<IDInfoKey, IDInformation> entrySet = (StoredEntrySet<IDInfoKey, IDInformation>)pool.entrySet();
		Iterator itor = entrySet.iterator();
		while(itor.hasNext())
		{
			Map.Entry<IDInfoKey,IDInformation> entry = (Map.Entry<IDInfoKey, IDInformation>)itor.next(); 
			IDInformation idi = (IDInformation) entry.getValue();
			HashMap<String, Integer> inFriends = idi.GetFriendsWhoKnownMe();
			IDGraphNode node = new IDGraphNode(entry.getKey().GetID());
			node.SetInEdges(inFriends);
			nodeMap.put(entry.getKey().GetID(), node);
		}
		itor = entrySet.iterator();
		while(itor.hasNext())
		{
			Map.Entry<IDInfoKey,IDInformation> entry = (Map.Entry<IDInfoKey, IDInformation>)itor.next(); 
			IDInformation idi = (IDInformation) entry.getValue();
			HashMap<String, Integer> inFriends = idi.GetFriendsWhoKnownMe();
			for(Map.Entry<String,Integer> friendentry:inFriends.entrySet())
			{
				IDGraphNode node = nodeMap.remove(friendentry.getKey());
				node.AddOutFriend(entry.getKey().GetID(), friendentry.getValue());
				nodeMap.put(friendentry.getKey(), node);
			}
		}
	}
	
	@Deprecated
	public IDGraph(HashMap<String, IDInformation> pool)
	{
		nodeMap = new HashMap<String, IDGraphNode>();
		for(Map.Entry<String, IDInformation> entry: pool.entrySet())
		{
			IDInformation idi = entry.getValue();
			HashMap<String, Integer> inFriends = idi.GetFriendsWhoKnownMe();
			IDGraphNode node = new IDGraphNode(entry.getKey());
			node.SetInEdges(inFriends);
			nodeMap.put(entry.getKey(), node);
		}
		for(Map.Entry<String, IDInformation> entry: pool.entrySet())
		{
			IDInformation idi = entry.getValue();
			HashMap<String, Integer> inFriends = idi.GetFriendsWhoKnownMe();
			for(Map.Entry<String,Integer> friendentry:inFriends.entrySet())
			{
				IDGraphNode node = nodeMap.remove(friendentry.getKey());
				node.AddOutFriend(entry.getKey(), friendentry.getValue());
				nodeMap.put(friendentry.getKey(), node);
			}
		}
	}
	
	public IDGraph(){nodeMap = new HashMap<String, IDGraphNode>();}
	public void SetNodeMap(HashMap<String, IDGraphNode> map){nodeMap = map;}
	public HashMap<String, IDGraphNode> GetNodeMap(){return nodeMap;}
	
	
	public void AddNode(String id)
	{
		if(nodeMap.containsKey(id))
			return;
		IDGraphNode node = new IDGraphNode(id);
		nodeMap.put(id, node);
	}
	
	public void AddEdge(String source,String sink)
	{
		if(source.equals(sink))
			return;
		if(!nodeMap.containsKey(source))
			return;
		if(!nodeMap.containsKey(sink))
			return;
		IDGraphNode sourceNode = nodeMap.remove(source);
		IDGraphNode sinkNode   = nodeMap.remove(sink);
		sourceNode.AddOutEdge(sink);
		sinkNode.AddInEdge(source);
		nodeMap.put(source, sourceNode);
		nodeMap.put(sink, sinkNode);
	}
}
